package com.dhcc.bpm.modules.alarm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.common.enums.ConfigType;
import com.dhcc.bpm.common.redisMessage.RedisMessageSender;
import com.dhcc.bpm.modules.alarm.entity.Alarm;
import com.dhcc.bpm.modules.alarm.service.IAlarmService;
import com.dhcc.bpm.modules.alarm.vo.AlarmVO;
import com.dhcc.bpm.modules.safe.service.IFlowService;
import com.dhcc.bpm.modules.system.service.ISerialNumberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Date;

/**
 * @ClassName AlarmController
 * @Description 告警表 控制器
 * @Author sjm
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@RestController
@AllArgsConstructor
@RequestMapping("bpm-alarm/alarm")
@Api(value = "告警表", tags = "告警表接口")
public class AlarmController extends BladeController {
	private static final Logger log = LoggerFactory.getLogger(AlarmController.class);
	private IAlarmService alarmService;
	private IFlowService flowService;
	private ISerialNumberService serialNumberService;
	@Autowired
	private RedisMessageSender redisMessageSender;
	/**
	 * @Description 详情
	 * @Author xiafayu
	 * @Date 2020/2/24 17:33
	 * @param alarm
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.alarm.entity.Alarm>
	 **/
	@ApiLog("告警详情")
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入alarm")
	@Log(title = "告警表接口",businessType = BusinessType.SELECT)
	public R<Alarm> detail(Alarm alarm) {
		Alarm detail = alarmService.getOne(Condition.getQueryWrapper(alarm));
		return R.data(detail);
	}

	/**
	 * @Description 分页 告警表
	 * @Author xiafayu
	 * @Date 2020/2/24 17:33
	 * @param alarm
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.alarm.entity.Alarm>>
	 **/
	@ApiLog("告警分页")
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入alarm")
	@Log(title = "告警表接口",businessType = BusinessType.SELECT)
	public R<IPage<Alarm>> list(Alarm alarm, Query query) {
		IPage<Alarm> pages = alarmService.page(Condition.getPage(query), Condition.getQueryWrapper(alarm).lambda().orderByDesc(Alarm::getCreateTime));
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 告警表
	 * @Author xiafayu
	 * @Date 2020/2/24 17:34
	 * @param alarm
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.alarm.vo.AlarmVO>>
	 **/
	@ApiLog("告警自定义分页")
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "自定义分页", notes = "传入alarm")
	@Log(title = "告警表接口",businessType = BusinessType.SELECT)
	public R<IPage<AlarmVO>> page(@ApiIgnore AlarmVO alarm, Query query) {
		IPage<AlarmVO> pages = alarmService.selectAlarmPage(Condition.getPage(query), alarm);
		return R.data(pages);
	}

	/**
	 * @Description 新增 告警表
	 * @Author xiafayu
	 * @Date 2020/2/24 17:34
	 * @param alarm
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("告警新增")
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入alarm")
	public R save(@Valid @RequestBody Alarm alarm) {
		return R.status(alarmService.save(alarm));
	}

	/**
	 * @Description 修改 告警表
	 * @Author xiafayu
	 * @Date 2020/2/24 17:34
	 * @param alarm
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("告警修改")
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入alarm")
	@Log(title = "告警表接口",businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody Alarm alarm) {
		return R.status(alarmService.updateById(alarm));
	}

	/**
	 * @Description 新增或修改 告警表
	 * @Author xiafayu
	 * @Date 2020/2/24 17:34
	 * @param alarm
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("告警新增修改")
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入alarm")
	@Log(title = "告警表接口",businessType = BusinessType.UPDATE)
	public R submit(@Valid @RequestBody Alarm alarm) {
		//1：未刷新
		alarm.setRefreshFlag(1);
		if (alarm.getId() == null){
            // 设置告警编码
            String code = serialNumberService.generateSerialNumberByModelCode("opc_alarm");
            alarm.setAlarmCode(code);
			alarm.setAlarmTime(new Date());
			//默认1 是待处理状态
			alarm.setStatus(1);
        }
		return R.status(alarmService.saveOrUpdate(alarm));
	}

	
	/**
	 * @Description 删除 告警表
	 * @Author xiafayu
	 * @Date 2020/2/24 17:34
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("告警删除")
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@Log(title = "告警表接口",businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(alarmService.deleteLogic(Func.toLongList(ids)));
	}

 /**
  * @Author wangjinghan
  * @Description 刷新限流配置
  * @param:
  * @return:
  * @date: 2020/4/24 14:24
  */
	@PostMapping("/refresh")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "刷新告警配置信息", notes = "刷新告警配置信息")
	@ApiLog("刷新告警配置信息")
	@Log(title = "刷新告警配置信息", businessType = BusinessType.REFRESH)
	public R refreshAlarm() {
		try {
			String message = redisMessageSender.format(ConfigType.ALARM.name(), "刷新告警配置信息");
			redisMessageSender.sendMessageToGateway("", message);
			Alarm alarm = new Alarm();
			//刷新状态---2：已刷新
			alarm.setRefreshFlag(2);
				int result =alarmService.updateRefreshFlag(alarm);
				if(result>=0){
					return R.status(true);
				}
				return R.status(false);

		}catch (Exception e){
			log.error("刷新限流配置失败:{}", e);
			return R.status(false);
		}
	}
}
